package com.czz.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.common.exception.CustomerException;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.manage.entity.Audit;
import com.czz.manage.entity.Course;
import com.czz.manage.entity.Teacher;
import com.czz.manage.entity.User;
import com.czz.manage.mapper.AuditMapper;
import com.czz.manage.mapper.CourseMapper;
import com.czz.manage.mapper.TeacherMapper;
import com.czz.manage.mapper.UserMapper;
import com.czz.manage.query.TeacherQuery;
import com.czz.manage.query.UserQuery;
import com.czz.manage.service.TeacherService;
import com.czz.manage.vo.TeacherVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @ClassName: TeacherServiceImpl
 * @Author Czz
 * @Date 2020/1/6
 * @Time 15:26
 **/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    AuditMapper auditMapper;

    @Override
    public void getTeacherPageList(Page<Teacher> pageParam, TeacherQuery teacherQuery) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("teacher_status");
        queryWrapper.orderByDesc("teacher_sort");
        queryWrapper.orderByDesc("gmt_create");

        if (ObjectUtils.isEmpty(teacherQuery)){
            teacherMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        Integer status = teacherQuery.getStatus();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("teacher_name",name);
        }

        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("teacher_level",level);
        }

        if (!StringUtils.isEmpty(status)){
            queryWrapper.eq("teacher_status",status);
        }

        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }

        teacherMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    @Transactional
    public Boolean deleteTeacherById(String id) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",id);
        Integer count = courseMapper.selectCount(queryWrapper);

        //如果存在课程
        if (count > 0){
            return false;
        }

        //删除讲师信息，将用户表是否讲师设置为0，删除审核表信息
        Teacher teacher = teacherMapper.selectById(id);
        User user = userMapper.selectById(teacher.getUserId());
        user.setTeacher(0);
        userMapper.updateById(user);

        QueryWrapper<Audit> auditQueryWrapper = new QueryWrapper<>();
        auditQueryWrapper.eq("user_id",user.getId());
        auditMapper.delete(auditQueryWrapper);

        teacherMapper.deleteById(id);
        return true;
    }

    @Override
    public void updateTeacherStatusById(String id,Integer status) {
        Teacher teacher = new Teacher();
        teacher.setTeacherId(id);
        teacher.setStatus(status);
        teacherMapper.updateById(teacher);
    }

    @Override
    public void updateTeacherById(Teacher teacher) {
        teacherMapper.updateById(teacher);
    }

    @Override
    public TeacherVo getTeacherUserById(String id) {
        TeacherVo teacherVo = new TeacherVo();
        Teacher teacher = teacherMapper.selectById(id);
        User user = userMapper.selectById(teacher.getUserId());

        BeanUtils.copyProperties(user,teacherVo);
        BeanUtils.copyProperties(teacher,teacherVo);
        return teacherVo;
    }
}
